package service.impl;

import dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.ContentService;
import util.BaseUtil;
import util.MessageUtil;
import util.ReturnResultUtil;
import view.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gable on 2018/5/23.
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    SearchDao searchDao;

    @Autowired
    ManningQuotasDao manningQuotasDao; //人员编制

    @Autowired
    PersonnelPerformanceDao personnelPerformanceDao; //人员绩效

    @Autowired
    MedicalBalanceDao medicalBalanceDao;  //收支情况

    @Autowired
    MedicalBusinessDao medicalBusinessDao; //医疗业务

    @Autowired
    RunningGuaranteeDao runningGuaranteeDao; //运行保障

    @Autowired
    FamilyPlanDao familyPlanDao;            //计生服务

    @Autowired
    GeneralDao generalDao;                  //全科医生


    @Override
    public ReturnResultUtil queryManningQuotas(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        //校验是否有去年年末的数据,如果有则拼接sql
        int count = searchDao.checkLastYearEnd((Integer) paramMap.get("lastYear"));
        if(count > 0){
            paramMap.put("cross",1);
        }
        List<ManningQuotasView> list;
        if(queryView.getHospitalId() != null){
            list = manningQuotasDao.queryForHospital(paramMap);
        }else{
            list = manningQuotasDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);

//        Long permanentStaffTotal = 0L;  //在编人数总计
//        Long lastYearEndStaffTotal = 0L;//去年末在编人数总计
//        Long nonStaffTotal = 0L;  //未在编人数总计
//        Long lastYearEndNonStaffTotal = 0L;  //去年末非在编人数总计
//        Long staffNumTotal = 0L;        //编制数总计
//
//        for(ManningQuotasView manningQuotasView : list){
//            Integer permanentStaff = manningQuotasView.getPermanentStaff();//在编人数(人)
//            Integer lastYearEndStaff = manningQuotasView.getLastYearEndStaff(); //去年末在编人数
//            Integer nonStaff = manningQuotasView.getNonStaff();     //未在编人数
//            Integer lastYearEndNonStaff = manningQuotasView.getLastYearEndNonStaff();   //去年末非在编人数
//            Integer staffNum = manningQuotasView.getStaffNum();     //编制数
//
//            permanentStaffTotal += permanentStaff==null?0:permanentStaff;
//            lastYearEndStaffTotal += lastYearEndStaff==null?0:lastYearEndStaff;
//            nonStaffTotal += nonStaff==null?0:nonStaff;
//            lastYearEndNonStaffTotal += lastYearEndNonStaff==null?0:lastYearEndNonStaff;
//            staffNumTotal += staffNum==null?0:staffNum;
//        }
//        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("list",list);
//        map.put("permanentStaffTotal",permanentStaffTotal);
//        map.put("lastYearEndStaffTotal",lastYearEndStaffTotal);
//        map.put("nonStaffTotal",nonStaffTotal);
//        map.put("lastYearEndNonStaffTotal",lastYearEndNonStaffTotal);
//        map.put("staffNumTotal",staffNumTotal);
//        return new ReturnResultUtil(1, MessageUtil.SUCCESS,map);
    }

    @Override
    public ReturnResultUtil queryPersonnelPerformance(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<PersonnelPerformanceView> list;
        if(queryView.getHospitalId() != null){
            list = personnelPerformanceDao.queryForHospital(paramMap);
        }else{
            list = personnelPerformanceDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryMedicalBalance(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<MedicalBalanceView> list;
        if(queryView.getHospitalId() != null){
            list = medicalBalanceDao.queryForHospital(paramMap);
            list = accumulativeMedicalBalance(list);
        }else{
            list = medicalBalanceDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    public List<MedicalBalanceView> accumulativeMedicalBalance(List<MedicalBalanceView> list){
        MedicalBalanceView total = null;
        if(list != null && list.size() > 0){
            MedicalBalanceView upMdicalBalanceView = null;  //上月数据用于计算
            for(MedicalBalanceView medicalBalanceView : list){
                Integer month = medicalBalanceView.getMonth();
                if(month == 1){
                    upMdicalBalanceView = (MedicalBalanceView) medicalBalanceView.clone();
                    continue;
                }
                Integer hospitalId = medicalBalanceView.getHospitalId();
                Integer upHospitalId = upMdicalBalanceView.getHospitalId();

                //计算合计
                if(hospitalId != null){
                    total = (MedicalBalanceView) medicalBalanceView.clone();
                }

                //计算上月累计
                Integer upMonth = upMdicalBalanceView.getMonth();
                if(month - upMonth == 1){
                    if(hospitalId != null && upHospitalId != null){
                        //这里copy一个对象，用于保存未计算的本月数据，作为下次的上月数据
                        MedicalBalanceView uppMdicalBalanceView = (MedicalBalanceView) medicalBalanceView.clone();

                        medicalBalanceView.setMedicalIncome(medicalBalanceView.getMedicalIncome() - upMdicalBalanceView.getMedicalIncome());
                        medicalBalanceView.setMedicareIncome(medicalBalanceView.getMedicareIncome() - upMdicalBalanceView.getMedicareIncome());
                        medicalBalanceView.setDrugIncome(medicalBalanceView.getDrugIncome() - upMdicalBalanceView.getDrugIncome());
                        medicalBalanceView.setMedicalExpenditure(medicalBalanceView.getMedicalExpenditure() - upMdicalBalanceView.getMedicalExpenditure());
                        medicalBalanceView.setHumanExpenditure(medicalBalanceView.getHumanExpenditure() - upMdicalBalanceView.getHumanExpenditure());
                        medicalBalanceView.setDrugExpenditure(medicalBalanceView.getDrugExpenditure() - upMdicalBalanceView.getDrugExpenditure());
                        medicalBalanceView.setMedicalBalance(medicalBalanceView.getMedicalBalance() - upMdicalBalanceView.getMedicalBalance());

                        upMdicalBalanceView = uppMdicalBalanceView;
                    }else{
                        upMdicalBalanceView = medicalBalanceView;
                    }
                }
            }
        }
        total.setMonth(13);
        total.setMonthName("合计");
        list.add(total);
        return list;
    }
    @Override
    public ReturnResultUtil queryRunningGuarantee(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<RunningGuaranteeView> list;
        if(queryView.getHospitalId() != null){
            list = runningGuaranteeDao.queryForHospital(paramMap);
            list = accumulativeRunningGuarantee(list);
        }else{
            list = runningGuaranteeDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    public List<RunningGuaranteeView> accumulativeRunningGuarantee(List<RunningGuaranteeView> list){
        RunningGuaranteeView total = null;
        if(list != null && list.size() > 0){
            RunningGuaranteeView upRunningGuaranteeView = null;  //上月数据用于计算
            for(RunningGuaranteeView runningGuaranteeView : list){
                Integer month = runningGuaranteeView.getMonth();
                if(month == 1){
                    upRunningGuaranteeView = (RunningGuaranteeView) runningGuaranteeView.clone();
                    continue;
                }
                Integer hospitalId = runningGuaranteeView.getHospitalId();
                Integer upHospitalId = upRunningGuaranteeView.getHospitalId();

                //计算合计
                if(hospitalId != null){
                    total = (RunningGuaranteeView) runningGuaranteeView.clone();
                }

                //计算上月累计
                Integer upMonth = upRunningGuaranteeView.getMonth();
                if(month - upMonth == 1){
                    if(hospitalId != null && upHospitalId != null){
                        //这里copy一个对象，用于保存未计算的本月数据，作为下次的上月数据
                        RunningGuaranteeView uppRunningGuaranteeView= (RunningGuaranteeView) runningGuaranteeView.clone();

                        if(runningGuaranteeView.getSafetyAccident() != null){
                            runningGuaranteeView.setSafetyAccident(runningGuaranteeView.getSafetyAccident() - (upRunningGuaranteeView.getSafetyAccident()==null?0:upRunningGuaranteeView.getSafetyAccident()));
                        }

                        if(runningGuaranteeView.getSmallProjectPractice() != null){
                            runningGuaranteeView.setSmallProjectPractice(runningGuaranteeView.getSmallProjectPractice() - (upRunningGuaranteeView.getSmallProjectPractice()==null?0:upRunningGuaranteeView.getSmallProjectPractice()));
                        }

                        upRunningGuaranteeView = uppRunningGuaranteeView;
                    }else{
                        upRunningGuaranteeView = runningGuaranteeView;
                    }
                }
            }
        }
        total.setMonth(13);
        total.setMonthName("合计");
        list.add(total);
        return list;
    }

    @Override
    public ReturnResultUtil queryFamilyPlan(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<FamilyPlanView> list;
        if(queryView.getHospitalId() != null){
            list = familyPlanDao.queryForHospital(paramMap);
            list = accumulativeFamilyPlan(list);
        }else{
            list = familyPlanDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    public List<FamilyPlanView> accumulativeFamilyPlan(List<FamilyPlanView> list){
        FamilyPlanView total = null;
        if(list != null && list.size() > 0){
            FamilyPlanView upFamilyPlanView = null;  //上月数据用于计算
            for(FamilyPlanView familyPlanView : list){
                Integer month = familyPlanView.getMonth();
                if(month == 1){
                    upFamilyPlanView = (FamilyPlanView) familyPlanView.clone();
                    continue;
                }
                Integer hospitalId = familyPlanView.getHospitalId();
                Integer upHospitalId = upFamilyPlanView.getHospitalId();

                //计算合计
                if(hospitalId != null){
                    total = (FamilyPlanView) familyPlanView.clone();
                }

                //计算上月累计
                Integer upMonth = upFamilyPlanView.getMonth();
                if(month - upMonth == 1){
                    if(hospitalId != null && upHospitalId != null){
                        //这里copy一个对象，用于保存未计算的本月数据，作为下次的上月数据
                        FamilyPlanView uppFamilyPlanView= (FamilyPlanView) familyPlanView.clone();

                        if(familyPlanView.getMidtermAbortion() != null){
                            familyPlanView.setMidtermAbortion(familyPlanView.getMidtermAbortion() - (upFamilyPlanView.getMidtermAbortion()==null?0:upFamilyPlanView.getMidtermAbortion()));
                        }
                        upFamilyPlanView = uppFamilyPlanView;
                    }else{
                        upFamilyPlanView = familyPlanView;
                    }
                }
            }
        }
        total.setMonth(13);
        total.setMonthName("合计");
        list.add(total);
        return list;
    }

    @Override
    public ReturnResultUtil queryMedicalBusiness(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<MedicalBusinessView> list;
        if(queryView.getHospitalId() != null){
            list = medicalBusinessDao.queryForHospital(paramMap);
            list = accumulativeMedicalBusiness(list);
        }else{
            list = medicalBusinessDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    public List<MedicalBusinessView> accumulativeMedicalBusiness(List<MedicalBusinessView> list){
        Map<String,Object> map = new HashMap<String,Object>();
        MedicalBusinessView total = null;
        if(list != null && list.size() > 0){
            MedicalBusinessView upMedicalBusinessView = null;  //上月数据用于计算
            for(MedicalBusinessView medicalBusinessView : list){
                Integer month = medicalBusinessView.getMonth();
                if(month == 1){
                    upMedicalBusinessView = (MedicalBusinessView) medicalBusinessView.clone();
                    continue;
                }
                Integer hospitalId = medicalBusinessView.getHospitalId();
                Integer upHospitalId = upMedicalBusinessView.getHospitalId();

                //计算合计
                if(hospitalId != null){
                    total = (MedicalBusinessView) medicalBusinessView.clone();
                }

                //计算上月累计
                Integer upMonth = upMedicalBusinessView.getMonth();
                if(month - upMonth == 1){
                    if(hospitalId != null && upHospitalId != null){
                        //这里copy一个对象，用于保存未计算的本月数据，作为下次的上月数据
                        MedicalBusinessView uppMedicalBusinessView = (MedicalBusinessView) medicalBusinessView.clone();

                        if(medicalBusinessView.getOutpatientPersontime() != null){
                            medicalBusinessView.setOutpatientPersontime(medicalBusinessView.getOutpatientPersontime() - (upMedicalBusinessView.getOutpatientPersontime()==null?0:upMedicalBusinessView.getOutpatientPersontime()));
                        }

                        if(medicalBusinessView.getDischarges() != null){
                            medicalBusinessView.setDischarges(medicalBusinessView.getDischarges() - (upMedicalBusinessView.getDischarges()==null?0:upMedicalBusinessView.getDischarges()));
                        }

                        if(medicalBusinessView.getActualSettlement() != null){
                            medicalBusinessView.setActualSettlement(medicalBusinessView.getActualSettlement() - (upMedicalBusinessView.getActualSettlement()==null?0:upMedicalBusinessView.getActualSettlement()));
                        }

                        if(medicalBusinessView.getMedicalEvent() != null){
                            medicalBusinessView.setMedicalEvent(medicalBusinessView.getMedicalEvent() - (upMedicalBusinessView.getMedicalEvent()==null?0:upMedicalBusinessView.getMedicalEvent()));
                        }

                        if(medicalBusinessView.getAdministrativePenaltyCases() != null){
                            medicalBusinessView.setAdministrativePenaltyCases(medicalBusinessView.getAdministrativePenaltyCases() - (upMedicalBusinessView.getAdministrativePenaltyCases()==null?0:upMedicalBusinessView.getAdministrativePenaltyCases()));
                        }

                        if(medicalBusinessView.getBloodPressure() != null){
                            medicalBusinessView.setBloodPressure(medicalBusinessView.getBloodPressure() - (upMedicalBusinessView.getBloodPressure()==null?0:upMedicalBusinessView.getBloodPressure()));
                        }

                        if(medicalBusinessView.getFeverClinicPersontime() != null){
                            medicalBusinessView.setFeverClinicPersontime(medicalBusinessView.getFeverClinicPersontime() - (upMedicalBusinessView.getFeverClinicPersontime()==null?0:upMedicalBusinessView.getFeverClinicPersontime()));
                        }

                        upMedicalBusinessView = uppMedicalBusinessView;
                    }else{
                        upMedicalBusinessView = medicalBusinessView;
                    }
                }
            }
        }
        total.setMonth(13);
        total.setMonthName("合计");
        list.add(total);
        return list;
    }
    @Override
    public ReturnResultUtil queryGeneral(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<GeneralView> list;
        if(queryView.getHospitalId() != null){
            list = generalDao.queryForHospital(paramMap);
        }else{
            list = generalDao.queryForHospitalType(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryThreeCore(QueryView queryView) {
        Map<String,Object> paramMap = BaseUtil.getParamMap(queryView);
        List<Map<String,Object>> list = null;
        if(queryView.getHospitalId() != null){
            list = generalDao.queryThreeCoreForHospital(paramMap);
        }else{
            list = generalDao.queryThreeCore(paramMap);
        }
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,list);
    }

    @Override
    public ReturnResultUtil queryMedicalIncomeRate(QueryView queryView) {
        Map paramMap = BaseUtil.getParamMap(queryView);
        List<Map<String, Object>> maps = medicalBalanceDao.queryMedicalIncomeRate(paramMap);
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,maps);
    }

    @Override
    public ReturnResultUtil queryDrugsIncomeRate(QueryView queryView) {
        Map paramMap = BaseUtil.getParamMap(queryView);
        List<Map<String, Object>> maps = medicalBalanceDao.queryDrugsIncomeRate(paramMap);
        return new ReturnResultUtil(1, MessageUtil.SUCCESS,maps);
    }


}
